package com.example.testapp.activity.activity.read;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RemoteViews;

import com.example.testapp.R;

/**
 * Created by l on 2016/7/28.
 */
public class NotificationBtnActivity extends AppCompatActivity {

    private int NOTIFICATION_ID_BASIC = 1;
    private int NOTIFICATION_ID_COLLAPSE = 2;
    private int NOTIFICATION_ID_HEADSUP = 3;
    private int NOTIFICATION_ID_VISIBILITY = 4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        initView();
    }

    private void initView() {
        findViewById(R.id.basicNotify).setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));

                PendingIntent pendingIntent = PendingIntent.getActivity(NotificationBtnActivity.this, 0, intent, 0);

                Notification.Builder builder = new Notification.Builder(NotificationBtnActivity.this);
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentIntent(pendingIntent);
                builder.setAutoCancel(true);
                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                builder.setContentTitle("Basic Notifications");
                builder.setContentText("I am a basic notification");
                builder.setSubText("it is really basic");

                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(NOTIFICATION_ID_BASIC, builder.build());
            }
        });

        findViewById(R.id.collapsedNotify).setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://www.sina.com"));
                PendingIntent pendingIntent = PendingIntent.getActivity(
                        NotificationBtnActivity.this, 0, intent, 0);
                Notification.Builder builder = new Notification.Builder(NotificationBtnActivity.this);
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentIntent(pendingIntent);
                builder.setAutoCancel(true);
                builder.setLargeIcon(BitmapFactory.decodeResource(
                        getResources(), R.mipmap.ic_launcher));
                // 通过RemoteViews来创建自定义的Notification视图
                RemoteViews contentView =
                        new RemoteViews(getPackageName(),
                                R.layout.notification);
                contentView.setTextViewText(R.id.textView,
                        "show me when collapsed");

                Notification notification = builder.build();
                notification.contentView = contentView;
                // 通过RemoteViews来创建自定义的Notification视图
                RemoteViews expandedView =
                        new RemoteViews(getPackageName(),
                                R.layout.notification_expanded);
                notification.bigContentView = expandedView;

                NotificationManager nm = (NotificationManager)
                        getSystemService(NOTIFICATION_SERVICE);
                nm.notify(NOTIFICATION_ID_COLLAPSE, notification);
            }
        });

        findViewById(R.id.headsupNotify).setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                Notification.Builder builder = new Notification.Builder(NotificationBtnActivity.this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setPriority(Notification.PRIORITY_DEFAULT)
                        .setCategory(Notification.CATEGORY_MESSAGE)
                        .setContentTitle("Headsup Notification")
                        .setContentText("I am a Headsup notification.");

                Intent push = new Intent();
                push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                push.setClass(NotificationBtnActivity.this, NotificationBtnActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(
                        NotificationBtnActivity.this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
                builder.setContentText("Heads-Up Notification on Android 5.0")
                        .setFullScreenIntent(pendingIntent, true);

                NotificationManager nm = (NotificationManager)
                        getSystemService(NOTIFICATION_SERVICE);
                nm.notify(NOTIFICATION_ID_HEADSUP, builder.build());
            }
        });

        findViewById(R.id.visibilityNotify).setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                RadioGroup radioGroup = (RadioGroup) findViewById(R.id.visibility_radio_group);
                Notification.Builder builder = new Notification.Builder(NotificationBtnActivity.this)
                        .setContentTitle("Notification for Visibility Test");
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.radio_button_public:
                        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
                        builder.setContentText("Public");
                        builder.setSmallIcon(R.mipmap.ic_public);
                        break;
                    case R.id.radio_button_private:
                        builder.setVisibility(Notification.VISIBILITY_PRIVATE);
                        builder.setContentText("Private");
                        builder.setSmallIcon(R.mipmap.ic_private);
                        break;
                    case R.id.radio_button_secret:
                        builder.setVisibility(Notification.VISIBILITY_SECRET);
                        builder.setContentText("Secret");
                        builder.setSmallIcon(R.mipmap.ic_secret);
                        break;
                }
                NotificationManager nm = (NotificationManager)
                        getSystemService(NOTIFICATION_SERVICE);
                nm.notify(NOTIFICATION_ID_VISIBILITY, builder.build());
            }
        });
    }
}
